package dorox.app;

import dorox.app.mq.event.RouteRequestEvent;
import dorox.app.util.MqUtil;
import org.apache.avro.util.Utf8;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Order(value=1)
public class ToolMain implements CommandLineRunner{
//	private static final Logger logger = LoggerFactory.getLogger(ToolMain.class);

    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
    static String table_prefix = "detail_report_";

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;


    String messageIds = "H10412H8AF3YTP4H";


    @Override
    public void run(String... args) throws Exception {
        String[] messageIdArray = messageIds.split(",");
        for (String messageId: messageIdArray){

            String table = getTableFromMessageId(messageId);
            Map<String, Object> map = jdbcTemplate.queryForMap(
                    "select DOWN_PORT_CODE, UP_PORT_CODE, DOWN_REGION_CODE, UP_REGION_CODE, MSG_ID, PHONE, CONTENT, SRC_ID from " +
                            table + " where message_Id='" + messageId + "'");

            String downName = (String)map.get("DOWN_PORT_CODE");
            String upName = (String)map.get("UP_PORT_CODE");
            String downRegionCode = (String)map.get("DOWN_REGION_CODE");
            String upRegionCode = (String)map.get("UP_REGION_CODE");
            String msgId = StringUtils.strip((String)map.get("MSG_ID"),"[]");

            List<Utf8> msgIds = new ArrayList<>();
            for(String mId : msgId.split(",")){
                msgIds.add(new Utf8(mId));
            }

            String phone = (String)map.get("PHONE");
            String content = (String)map.get("CONTENT");
            if("799899".equals(downName)){
                content = "【湖北电力】" + content;
            }
            String srcIds = (String)map.get("SRC_ID");
            String[] srcIdArray = srcIds.split("/");
            String srcId = srcIdArray[0], upSrcId=srcIdArray[1];

            RouteRequestEvent routeRequestEvent = new RouteRequestEvent(messageId, downName,
                    upName, phone, content, srcId, upSrcId, msgIds,
                    downRegionCode, upRegionCode);
            MqUtil.sendMsg(routeRequestEvent, rabbitTemplate, upRegionCode);
        }

    }

    private String getTableFromMessageId(String messageId) {
        String y = sdf.format(new Date());
        String md = messageId.substring(2,6);
        return table_prefix + y + md;
    }
}

